﻿using System;
using System.Collections.Generic;
using System.Text;
using MobileBroadbandUserBlog.MBLM.Core.Interfaces;
using MobileBroadbandUserBlog.MBLM.Core.Interfaces;

namespace MobileBroadbandUserBlog.MBLM.Core
{
    public class ChangeStatistics : IChangeStatistics
    {
        private IDateTimeContainer _dateTimeContainer;

        public ChangeStatistics(IDateTimeContainer dateTimeContainer)
        {
            _dateTimeContainer = dateTimeContainer;
        }


        List<DateTime> _registrations = new List<DateTime>();

        #region IChangeStatistics Members

        public override void AddToStatistics(DateTime registrationTime)
        {
            _registrations.Add(registrationTime);
            AdjustRegistrations();
        }

        private void AdjustRegistrations()
        {
            List<DateTime> _listOfDatesToRemove = _registrations.FindAll(delegate(DateTime obj)
            {
                DateTime exceedTimeLimit = _dateTimeContainer.GetNow().AddMinutes(-1);
                if (obj < exceedTimeLimit)
                {
                    return true;
                }
                return false;
            });

            foreach (DateTime date in _listOfDatesToRemove)
            {
                _registrations.Remove(date);
            }

        }


        public override int ChangesPerMinute
        {
            get { return _registrations.Count; }
        }

        public override int ChangesPerHour
        {
            get { return _registrations.Count*60; }
        }

        public override int ChangesPerWorkday
        {
            get { return _registrations.Count*60*8; }
        }


        public override void ResetStatistics()
        {
            _registrations.Clear();
        }

        #endregion
    }
}
